{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Vowel Spellchecker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: spellchecker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #元音拼写检查器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在给定单词列表&nbsp;<code>wordlist</code>&nbsp;的情况下，我们希望实现一个拼写检查器，将查询单词转换为正确的单词。</p>\n",
    "\n",
    "<p>对于给定的查询单词&nbsp;<code>query</code>，拼写检查器将会处理两类拼写错误：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>大小写：如果查询匹配单词列表中的某个单词（<strong>不区分大小写</strong>），则返回的正确单词与单词列表中的大小写相同。\n",
    "\n",
    "\t<ul>\n",
    "\t\t<li>例如：<code>wordlist = [\"yellow\"]</code>, <code>query = \"YellOw\"</code>: <code>correct = \"yellow\"</code></li>\n",
    "\t\t<li>例如：<code>wordlist = [\"Yellow\"]</code>, <code>query = \"yellow\"</code>: <code>correct = \"Yellow\"</code></li>\n",
    "\t\t<li>例如：<code>wordlist = [\"yellow\"]</code>, <code>query = \"yellow\"</code>: <code>correct = \"yellow\"</code></li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>元音错误：如果在将查询单词中的元音 <code>('a', 'e', 'i', 'o', 'u')</code>&nbsp;&nbsp;分别替换为任何元音后，能与单词列表中的单词匹配（<strong>不区分大小写</strong>），则返回的正确单词与单词列表中的匹配项大小写相同。\n",
    "\t<ul>\n",
    "\t\t<li>例如：<code>wordlist = [\"YellOw\"]</code>, <code>query = \"yollow\"</code>: <code>correct = \"YellOw\"</code></li>\n",
    "\t\t<li>例如：<code>wordlist = [\"YellOw\"]</code>, <code>query = \"yeellow\"</code>: <code>correct = \"\"</code> （无匹配项）</li>\n",
    "\t\t<li>例如：<code>wordlist = [\"YellOw\"]</code>, <code>query = \"yllw\"</code>: <code>correct = \"\"</code> （无匹配项）</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>此外，拼写检查器还按照以下优先级规则操作：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>当查询完全匹配单词列表中的某个单词（<strong>区分大小写</strong>）时，应返回相同的单词。</li>\n",
    "\t<li>当查询匹配到大小写问题的单词时，您应该返回单词列表中的第一个这样的匹配项。</li>\n",
    "\t<li>当查询匹配到元音错误的单词时，您应该返回单词列表中的第一个这样的匹配项。</li>\n",
    "\t<li>如果该查询在单词列表中没有匹配项，则应返回空字符串。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给出一些查询 <code>queries</code>，返回一个单词列表 <code>answer</code>，其中 <code>answer[i]</code> 是由查询 <code>query = queries[i]</code> 得到的正确单词。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>wordlist = [\"KiTe\",\"kite\",\"hare\",\"Hare\"], queries = [\"kite\",\"Kite\",\"KiTe\",\"Hare\",\"HARE\",\"Hear\",\"hear\",\"keti\",\"keet\",\"keto\"]\n",
    "<strong>输出：</strong>[\"kite\",\"KiTe\",\"KiTe\",\"Hare\",\"hare\",\"\",\"\",\"KiTe\",\"\",\"KiTe\"]</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>wordlist = [\"yellow\"], queries = [\"YellOw\"]\n",
    "<b>输出：</b>[\"yellow\"]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= wordlist.length, queries.length &lt;= 5000</code></li>\n",
    "\t<li><code>1 &lt;= wordlist[i].length, queries[i].length &lt;= 7</code></li>\n",
    "\t<li><code>wordlist[i]</code>&nbsp;和&nbsp;<code>queries[i]</code>&nbsp;只包含英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [vowel-spellchecker](https://leetcode.cn/problems/vowel-spellchecker/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [vowel-spellchecker](https://leetcode.cn/problems/vowel-spellchecker/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"KiTe\",\"kite\",\"hare\",\"Hare\"]\\n[\"kite\",\"Kite\",\"KiTe\",\"Hare\",\"HARE\",\"Hear\",\"hear\",\"keti\",\"keet\",\"keto\"]', '[\"yellow\"]\\n[\"YellOw\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        \"\"\"\n",
    "        def dps(wordlist, query):\n",
    "            if query in wordlist:###匹配到相同的单词\n",
    "                return query\n",
    "            final = \"\"\n",
    "            target = query.lower()\n",
    "            for item in wordlist:\n",
    "                temp = item.lower()\n",
    "                #n = len(temp)\n",
    "                \n",
    "                if temp == target:\n",
    "                    return item\n",
    "\n",
    "            for item in wordlist:\n",
    "                temp = item.lower()\n",
    "                n = len(temp)\n",
    "                #target = query.lower()\n",
    "                if n != len(target):\n",
    "                    return final\n",
    "                lst1 = []\n",
    "                lst2 = []\n",
    "                lst11 = []\n",
    "                lst22 = []\n",
    "                for i in range(n):\n",
    "                    if temp[i] in ['a', 'e', 'i', 'o', 'u']:\n",
    "                        lst1.append(i)\n",
    "                    else:\n",
    "                        lst11.append(temp[i])\n",
    "                    if target[i] in ['a', 'e', 'i', 'o', 'u']:\n",
    "                        lst2.append(i)\n",
    "                    else:\n",
    "                        lst22.append(target[i])\n",
    "                if lst1 == lst2 and lst11 == lst22:\n",
    "                    return item\n",
    "\n",
    "            return final\n",
    "        result = []\n",
    "        for query in queries:\n",
    "            result.append(dps(wordlist, query))\n",
    "        return result\n",
    "        \"\"\"\n",
    "        ans = ['' for q in queries]\n",
    "        lower_wordlist = [w.lower() for w in wordlist]\n",
    "        vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', w) for w in lower_wordlist]\n",
    "        for i, q in enumerate(queries):\n",
    "            lower_q = q.lower()\n",
    "            vowel_q = re.sub(r'[a|e|i|o|u]', 'a', lower_q)\n",
    "            if q in wordlist:\n",
    "                ans[i] = q\n",
    "            elif lower_q in lower_wordlist:\n",
    "                ans[i] = wordlist[lower_wordlist.index(lower_q)]\n",
    "            elif vowel_q in vowel_wordlist:\n",
    "                ans[i] = wordlist[vowel_wordlist.index(vowel_q)]\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        ans = [\"\" for _ in queries]\n",
    "        lower_words = [s.lower() for s in wordlist]\n",
    "        vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', s) for s in lower_words]\n",
    "\n",
    "        for i, q in enumerate(queries):\n",
    "            lq = q.lower()\n",
    "            vq = re.sub(r'[a|e|i|o|u]', 'a', lq)\n",
    "            if q in wordlist:\n",
    "                idx = wordlist.index(q)\n",
    "                ans[i] = wordlist[idx]\n",
    "            elif lq in lower_words:\n",
    "                idx = lower_words.index(lq)\n",
    "                ans[i] = wordlist[idx]\n",
    "            elif vq in vowel_wordlist:\n",
    "                idx = vowel_wordlist.index(vq)\n",
    "                ans[i] = wordlist[idx]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return \"\".join('*' if c in 'aeiou' else c for c in word)\n",
    "\n",
    "        words_perfect = set(wordlist)\n",
    "        words_cap = {}\n",
    "        words_vow = {}\n",
    "\n",
    "        for word in wordlist:\n",
    "            wordlow = word.lower()\n",
    "            words_cap.setdefault(wordlow, word)\n",
    "            words_vow.setdefault(devowel(wordlow), word)\n",
    "\n",
    "        # print(words_perfect)\n",
    "        # print(words_cap)\n",
    "        # print(words_vow)\n",
    "        def solve(query):\n",
    "            if query in words_perfect:\n",
    "                return query\n",
    "\n",
    "            queryL = query.lower()\n",
    "            if queryL in words_cap:\n",
    "                return words_cap[queryL]\n",
    "\n",
    "            queryLV = devowel(queryL)\n",
    "            if queryLV in words_vow:\n",
    "                return words_vow[queryLV]\n",
    "            return \"\"\n",
    "\n",
    "        return list(map(solve, queries))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        ans = ['' for q in queries]\n",
    "        lower_wordlist = [w.lower() for w in wordlist]\n",
    "        vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', w) for w in lower_wordlist]\n",
    "        for i, q in enumerate(queries):\n",
    "            lower_q = q.lower()\n",
    "            vowel_q = re.sub(r'[a|e|i|o|u]', 'a', lower_q)\n",
    "            if q in wordlist:\n",
    "                ans[i] = q\n",
    "            elif lower_q in lower_wordlist:\n",
    "                ans[i] = wordlist[lower_wordlist.index(lower_q)]\n",
    "            elif vowel_q in vowel_wordlist:\n",
    "                ans[i] = wordlist[vowel_wordlist.index(vowel_q)]\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        ans = ['' for q in queries]\n",
    "        lower_wordlist = [w.lower() for w in wordlist]\n",
    "        vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', w) for w in lower_wordlist]\n",
    "        for i, q in enumerate(queries):\n",
    "            lower_q = q.lower()\n",
    "            vowel_q = re.sub(r'[a|e|i|o|u]', 'a', lower_q)\n",
    "            if q in wordlist:\n",
    "                ans[i] = q\n",
    "            elif lower_q in lower_wordlist:\n",
    "                ans[i] = wordlist[lower_wordlist.index(lower_q)]\n",
    "            elif vowel_q in vowel_wordlist:\n",
    "                ans[i] = wordlist[vowel_wordlist.index(vowel_q)]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        word2=[]\n",
    "        word1=[]\n",
    "\n",
    "        for i in wordlist:\n",
    "            i=i.lower()\n",
    "            word1.append(i)\n",
    "            temp=''\n",
    "            for j in i:\n",
    "                if j in ['a','e','i','o','u']:\n",
    "                    temp+='a'\n",
    "                else:\n",
    "                    temp+=j\n",
    "            word2.append(temp)\n",
    "\n",
    "        #return word1\n",
    "\n",
    "        result=[]\n",
    "        \n",
    "        for i in queries:\n",
    "            if i in wordlist:\n",
    "                result.append(i)\n",
    "              \n",
    "            elif i.lower() in word1:\n",
    "                i=i.lower()\n",
    "                result.append(wordlist[word1.index(i)])\n",
    "            else:\n",
    "                i=i.lower()\n",
    "                i=i.replace('e','a')\n",
    "                i=i.replace('i','a')\n",
    "                i=i.replace('o','a')\n",
    "                i=i.replace('u','a')\n",
    "            #try1.append(i)\n",
    "            #return i\n",
    "                if i in word2:\n",
    "                    aa=wordlist[word2.index(i)]\n",
    "                    result.append(aa)\n",
    "                else:\n",
    "                    result.append('')\n",
    "            \n",
    "        return result\n",
    "        #return try1\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        ans = ['' for q in queries]\n",
    "        lower_wordlist = [w.lower() for w in wordlist]\n",
    "        vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', w) for w in lower_wordlist]\n",
    "        for i, q in enumerate(queries):\n",
    "            lower_q = q.lower()\n",
    "            vowel_q = re.sub(r'[a|e|i|o|u]', 'a', lower_q)\n",
    "            if q in wordlist:\n",
    "                ans[i] = q\n",
    "            elif lower_q in lower_wordlist:\n",
    "                ans[i] = wordlist[lower_wordlist.index(lower_q)]\n",
    "            elif vowel_q in vowel_wordlist:\n",
    "                ans[i] = wordlist[vowel_wordlist.index(vowel_q)]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def change_word(word: str) -> str:\n",
    "            temp=''\n",
    "            for ch in word:\n",
    "                if ch in ['a','e','i','o','u']:\n",
    "                    temp+='*'\n",
    "                else:\n",
    "                    temp+=ch\n",
    "            return temp\n",
    "\n",
    "        res=[]\n",
    "        lower_wordlist=[]\n",
    "        for word in wordlist:\n",
    "            lower_wordlist.append(word.lower())\n",
    "        change_wordlist=[]\n",
    "        for word in lower_wordlist:\n",
    "            change_wordlist.append(change_word(word))\n",
    "        \n",
    "        for i, query in enumerate(queries):\n",
    "            if query in wordlist:\n",
    "                res.append(query)\n",
    "            elif query.lower() in lower_wordlist:\n",
    "                index=lower_wordlist.index(query.lower())\n",
    "                res.append(wordlist[index])\n",
    "            elif change_word(query.lower()) in change_wordlist:\n",
    "                index=change_wordlist.index(change_word(query.lower()))\n",
    "                res.append(wordlist[index])\n",
    "            else:\n",
    "                res.append('')\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "\n",
    "\n",
    "        def check(word):\n",
    "            st = \"\"\n",
    "            for w in word:\n",
    "                if w.lower() in target:\n",
    "                    st += \"*\"\n",
    "                else:\n",
    "                    st += w.lower()\n",
    "            return st\n",
    "\n",
    "        dct1 = dict()\n",
    "        target = set(\"aeiou\")\n",
    "        for word in wordlist:\n",
    "            if word not in dct1:\n",
    "                dct1[word] = word\n",
    "        dct2 = dict()\n",
    "        for word in wordlist:\n",
    "            if word.lower() not in dct2:\n",
    "                dct2[word.lower()] = word\n",
    "        dct3 = dict()\n",
    "        for word in wordlist:\n",
    "            st = check(word)\n",
    "            if st not in dct3:\n",
    "                dct3[st] = word\n",
    "\n",
    "        ans = []\n",
    "        for word in queries:\n",
    "            if word in dct1:\n",
    "                ans.append(dct1[word])\n",
    "            elif word.lower() in dct2:\n",
    "                ans.append(dct2[word.lower()])\n",
    "            else:\n",
    "                st = check(word)\n",
    "                if st in dct3:\n",
    "                    ans.append(dct3[st])\n",
    "                else:\n",
    "                    ans.append(\"\")\n",
    " \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        Words, Words_change, res = set(wordlist), {}, []\n",
    "        for word in wordlist:\n",
    "            word_low = word.lower()\n",
    "            word_ex = ''.join([w if w not in 'aeiou' else '_' for w in word_low])\n",
    "            if word_low not in Words_change:\n",
    "                Words_change[word_low] = word\n",
    "\n",
    "            if word_ex not in Words_change:\n",
    "                Words_change[word_ex] = word\n",
    "\n",
    "        for q in queries:\n",
    "            q_low = q.lower()\n",
    "            q_ex = ''.join([w if w not in 'aeiou' else '_' for w in q_low])\n",
    "            if q in Words:\n",
    "                res.append(q)\n",
    "            elif q_low in Words_change:\n",
    "                res.append(Words_change[q_low])\n",
    "            elif q_ex in Words_change:\n",
    "                res.append(Words_change[q_ex])\n",
    "            else:\n",
    "                res.append('')\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        s = set(wordlist)\n",
    "        ans = []\n",
    "        cap = {}\n",
    "        dev = {}\n",
    "        vowels = {'a','e','i','o','u'}\n",
    "\n",
    "        for word in wordlist:\n",
    "            cap.setdefault(word.lower(), word)\n",
    "            d = ''.join('*' if c in vowels else c for c in word.lower())\n",
    "            dev.setdefault(d, word)\n",
    "\n",
    "        def check(query):\n",
    "            if query in s:\n",
    "                return query\n",
    "            query = query.lower()\n",
    "            if query in cap:\n",
    "                return cap[query]\n",
    "            d = ''.join('*' if c in vowels else c for c in query.lower())\n",
    "            if d in dev:\n",
    "                return dev[d]\n",
    "            return ''\n",
    "                    \n",
    "\n",
    "\n",
    "        for query in queries:\n",
    "            ans.append(check(query))\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        Words, Words_change = set(wordlist), {}\n",
    "        for word in wordlist:\n",
    "            word_low = word.lower()\n",
    "            word_ex = ''.join([w if w not in 'aeiou' else '_' for w in word_low])\n",
    "            if word_low not in Words_change: Words_change[word_low] = word\n",
    "            if word_ex not in Words_change: Words_change[word_ex] = word\n",
    "\n",
    "        def resolve(s):\n",
    "            if s in Words: return s\n",
    "\n",
    "            s_low = s.lower()\n",
    "            if s_low in Words_change: return Words_change[s_low]\n",
    "\n",
    "            s_ex = ''.join([w if w not in 'aeiou' else '_' for w in s_low])\n",
    "            if s_ex in Words_change: return Words_change[s_ex]\n",
    "\n",
    "            return ''\n",
    "\n",
    "\n",
    "        return [resolve(q) for q in queries]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def tran1(x):\n",
    "            r = 0\n",
    "            for i in x:\n",
    "                if ord(i) < 91:\n",
    "                    r *= 100\n",
    "                    r +=ord(i)\n",
    "                else:\n",
    "                    r *= 100\n",
    "                    r +=ord(i)-32\n",
    "            return r\n",
    "        def tran2(x):\n",
    "            r = 0\n",
    "            for i in x:\n",
    "                if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u' or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U':\n",
    "                    r *= 10\n",
    "                elif ord(i) < 91:\n",
    "                    r *= 100\n",
    "                    r +=ord(i)\n",
    "                else:\n",
    "                    r *= 100\n",
    "                    r +=ord(i)-32\n",
    "            return r\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        dic3 = {}\n",
    "        for i in wordlist:\n",
    "            dic1[i] = 1\n",
    "            j = tran1(i)\n",
    "            if j not in dic2:\n",
    "                dic2[j] = i\n",
    "            k = tran2(i)\n",
    "            if k not in dic3:\n",
    "                dic3[k] = i\n",
    "        n = len(queries)\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if queries[i] in wordlist:\n",
    "                ans.append(queries[i])\n",
    "            elif tran1(queries[i]) in dic2:\n",
    "                ans.append(dic2[tran1(queries[i])])\n",
    "            elif tran2(queries[i]) in dic3:\n",
    "                ans.append(dic3[tran2(queries[i])])\n",
    "            else:\n",
    "                ans.append(\"\")\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        Words = set(wordlist)\n",
    "        Words_change = {}\n",
    "        for word in wordlist:\n",
    "            \n",
    "            word_low = word.lower()\n",
    "            if word_low not in Words_change:\n",
    "                Words_change[word_low] = word\n",
    "\n",
    "            word_ex = ''.join([w if w not in 'aeiou' else '_' for w in word_low])\n",
    "            if word_ex not in Words_change:\n",
    "                Words_change[word_ex] = word\n",
    "\n",
    "            #print(word, word_low, word_ex)\n",
    "\n",
    "        #print(Words_change)\n",
    "\n",
    "\n",
    "        res = []\n",
    "        for q in queries:\n",
    "            if q in Words:\n",
    "                res.append(q)\n",
    "                continue\n",
    "\n",
    "            q_low = q.lower()\n",
    "            if q_low in Words_change:\n",
    "                res.append(Words_change[q_low])\n",
    "                continue\n",
    "\n",
    "            q_ex = ''.join([w if w not in 'aeiou' else '_' for w in q_low])\n",
    "            if q_ex in Words_change:\n",
    "                res.append(Words_change[q_ex])\n",
    "                continue\n",
    "\n",
    "            res.append('')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        wordSet = set(wordlist)\n",
    "        wordDict = collections.defaultdict(dict)\n",
    "        checkSet = set(\"aeiouAEIOU\")\n",
    "        for word in wordlist:\n",
    "            temp = []\n",
    "            for ch in word:\n",
    "                if ch in checkSet:  temp.append(\"*\")\n",
    "                elif \"A\" <= ch <= \"Z\":  temp.append(chr(ord(ch)-ord(\"A\")+ord(\"a\")))\n",
    "                else:   temp.append(ch)\n",
    "            temp = \"\".join(temp)\n",
    "            if temp not in wordDict[len(word)]:\n",
    "                wordDict[len(word)][temp] = word\n",
    "            temp = word.lower()\n",
    "            if temp not in wordDict:    wordDict[temp] = word\n",
    "\n",
    "        for i in range(len(queries)):\n",
    "            if queries[i] not in wordSet:\n",
    "                if len(queries[i]) not in wordDict:\n",
    "                    queries[i] = \"\"\n",
    "                    continue\n",
    "                temp = queries[i].lower()\n",
    "                if temp in wordDict:    \n",
    "                    queries[i] = wordDict[temp]\n",
    "                    continue\n",
    "                temp = []\n",
    "                for ch in queries[i]:\n",
    "                    if ch in checkSet:  temp.append(\"*\")\n",
    "                    elif \"A\" <= ch <= \"Z\":  temp.append(chr(ord(ch)-ord(\"A\")+ord(\"a\")))\n",
    "                    else:   temp.append(ch)\n",
    "                temp = \"\".join(temp)\n",
    "                if temp not in wordDict[len(temp)]:\n",
    "                    queries[i] = \"\"\n",
    "                    continue\n",
    "                queries[i] = wordDict[len(temp)][temp]\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        perfect_match_set = set()       # 完美匹配\n",
    "        lowercase_match_dict = dict()   # 大小写匹配\n",
    "        vowel_match_dict = dict()       # 元音匹配。统一把元音转为最小字典序a，降低检索空间\n",
    "        for word in wordlist:\n",
    "            perfect_match_set.add(word)\n",
    "            word_lower = word.lower()\n",
    "            if word_lower not in lowercase_match_dict:\n",
    "                lowercase_match_dict[word_lower] = word\n",
    "            vowel_minimum_lexicographic_order_word = vowel_minimum_lexicographic_order(word)\n",
    "            if vowel_minimum_lexicographic_order_word not in vowel_match_dict:\n",
    "                vowel_match_dict[vowel_minimum_lexicographic_order_word] = word\n",
    "        answer_list = [''] * len(queries)\n",
    "        for index in range(len(queries)):\n",
    "            if queries[index] in perfect_match_set:\n",
    "                answer_list[index] = queries[index]\n",
    "                continue\n",
    "            query_lower = queries[index].lower()\n",
    "            if query_lower in lowercase_match_dict:\n",
    "                answer_list[index] = lowercase_match_dict[query_lower]\n",
    "                continue \n",
    "            vowel_minimum_lexicographic_order_word = vowel_minimum_lexicographic_order(queries[index])\n",
    "            if vowel_minimum_lexicographic_order_word in vowel_match_dict:\n",
    "                answer_list[index] = vowel_match_dict[vowel_minimum_lexicographic_order_word]\n",
    "                continue \n",
    "        return answer_list\n",
    "\n",
    "def vowel_minimum_lexicographic_order(word):\n",
    "    word_lower = word.lower()\n",
    "    answer_list = list(word_lower)\n",
    "    vowel_set = {'a', 'e', 'i', 'o', 'u'}\n",
    "    for index in range(len(word_lower)):\n",
    "        if word_lower[index] in vowel_set:\n",
    "            answer_list[index] = 'a'\n",
    "    return ''.join(answer_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "\n",
    "        def devowel(word): \n",
    "            return \"\".join('*' if c in 'aeiou' else c\n",
    "                            for c in word)\n",
    "        \n",
    "        words_perfect = set(wordlist)\n",
    "        words_cap = {}\n",
    "        words_vow = {}\n",
    "\n",
    "        for word in wordlist: \n",
    "            wordlow = word.lower()\n",
    "            words_cap.setdefault(wordlow, word)\n",
    "            words_vow.setdefault(devowel(wordlow), word)\n",
    "        \n",
    "        def solve(query): \n",
    "            if query in words_perfect:\n",
    "                return query\n",
    "            \n",
    "            queryL = query.lower()\n",
    "            if queryL in words_cap:\n",
    "                return words_cap[queryL]\n",
    "\n",
    "            queryLV = devowel(queryL)\n",
    "\n",
    "            if queryLV in words_vow: \n",
    "                return words_vow[queryLV]\n",
    "\n",
    "            return \"\"\n",
    "        return list(map(solve,queries)) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        wordSet = set(wordlist)\n",
    "        wordDict = collections.defaultdict(dict)\n",
    "        checkSet = set(\"aeiouAEIOU\")\n",
    "        for word in wordlist:\n",
    "            temp = []\n",
    "            for ch in word:\n",
    "                if ch in checkSet:  temp.append(\"*\")\n",
    "                elif \"A\" <= ch <= \"Z\":  temp.append(chr(ord(ch)-ord(\"A\")+ord(\"a\")))\n",
    "                else:   temp.append(ch)\n",
    "            temp = \"\".join(temp)\n",
    "            if temp not in wordDict[len(word)]:\n",
    "                wordDict[len(word)][temp] = word\n",
    "            temp = word.lower()\n",
    "            if temp not in wordDict:    wordDict[temp] = word\n",
    "\n",
    "        for i in range(len(queries)):\n",
    "            if queries[i] not in wordSet:\n",
    "                if len(queries[i]) not in wordDict:\n",
    "                    queries[i] = \"\"\n",
    "                    continue\n",
    "                temp = queries[i].lower()\n",
    "                if temp in wordDict:    \n",
    "                    queries[i] = wordDict[temp]\n",
    "                    continue\n",
    "                temp = []\n",
    "                for ch in queries[i]:\n",
    "                    if ch in checkSet:  temp.append(\"*\")\n",
    "                    elif \"A\" <= ch <= \"Z\":  temp.append(chr(ord(ch)-ord(\"A\")+ord(\"a\")))\n",
    "                    else:   temp.append(ch)\n",
    "                temp = \"\".join(temp)\n",
    "                if temp not in wordDict[len(temp)]:\n",
    "                    queries[i] = \"\"\n",
    "                    continue\n",
    "                queries[i] = wordDict[len(temp)][temp]\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return \"\".join('*' if c in 'aeiou' else c\n",
    "                           for c in word)\n",
    "        words_perfect = set(wordlist)\n",
    "        words_cap = {}\n",
    "        words_vow = {}\n",
    "        for word in wordlist:\n",
    "            wordlow = word.lower()\n",
    "            words_cap.setdefault(wordlow, word)\n",
    "            words_vow.setdefault(devowel(wordlow), word)\n",
    "        def solve(query):\n",
    "            if query in words_perfect:\n",
    "                return query\n",
    "            queryL = query.lower()\n",
    "            if queryL in words_cap:\n",
    "                return words_cap[queryL]\n",
    "            queryLV = devowel(queryL)\n",
    "            if queryLV in words_vow:\n",
    "                return words_vow[queryLV]\n",
    "            return \"\"\n",
    "        return list(map(solve, queries))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return \"\".join('*' if c in 'aeiou' else c for c in word)\n",
    "        word_perfect = set(wordlist)\n",
    "        word_cap = {}\n",
    "        word_vow = {}\n",
    "        for word in wordlist:\n",
    "            wordlow = word.lower()\n",
    "            word_cap.setdefault(wordlow, word)\n",
    "            word_vow.setdefault(devowel(wordlow), word)\n",
    "        def solve(query):\n",
    "            if query in word_perfect:\n",
    "                return query\n",
    "            queryl = query.lower()\n",
    "            if queryl in word_cap:\n",
    "                return word_cap[queryl]\n",
    "            querylV = devowel(queryl)\n",
    "            if querylV in word_vow:\n",
    "                return word_vow[querylV]\n",
    "            return \"\"\n",
    "        return [solve(query) for query in queries]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return ''.join('*' if c in 'aeiou' else c for c in word)\n",
    "        \n",
    "        def solve(query):\n",
    "            if query in word_perfect:\n",
    "                return query\n",
    "            \n",
    "            query_lower = query.lower()\n",
    "            if query_lower in word_cap:\n",
    "                return word_cap[query_lower]\n",
    "            \n",
    "            query_vowel = devowel(query_lower)\n",
    "            if query_vowel in word_vowel:\n",
    "                return word_vowel[query_vowel]\n",
    "            \n",
    "            return \"\"\n",
    "        \n",
    "        word_perfect = set(wordlist)\n",
    "        word_cap = {}\n",
    "        word_vowel = {}\n",
    "\n",
    "        for word in wordlist:\n",
    "            word_lower = word.lower()\n",
    "            word_cap.setdefault(word_lower, word)\n",
    "            word_vowel.setdefault(devowel(word_lower), word)\n",
    "\n",
    "        return [solve(query) for query in queries]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        wordset = set(wordlist)\n",
    "\n",
    "        wordlower = {word.lower(): word for word in wordlist[::-1]}\n",
    "        wordvowel = {\n",
    "            re.sub(r\"[aeiou]\", \"a\", word.lower()): word for word in wordlist[::-1]\n",
    "        }\n",
    "\n",
    "        # 当查询完全匹配单词列表中的某个单词（区分大小写）时，应返回相同的单词。\n",
    "        # 当查询匹配到大小写问题的单词时，您应该返回单词列表中的第一个这样的匹配项。\n",
    "        # 当查询匹配到元音错误的单词时，您应该返回单词列表中的第一个这样的匹配项。\n",
    "        # 如果该查询在单词列表中没有匹配项，则应返回空字符串。\n",
    "        res = []\n",
    "        for word in queries:\n",
    "            if word in wordset:\n",
    "                res.append(word)\n",
    "            elif word.lower() in wordlower:\n",
    "                res.append(wordlower[word.lower()])\n",
    "            else:\n",
    "                word = re.sub(r\"[aeiou]\", \"a\", word.lower())\n",
    "                if word in wordvowel:\n",
    "                    res.append(wordvowel[word])\n",
    "                else:\n",
    "                    res.append(\"\")\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return \"\".join('*' if c in 'aeiou' else c\n",
    "                           for c in word)\n",
    "        wwwww=set(wordlist)\n",
    "        wc={}\n",
    "        wn={}\n",
    "        for each in wordlist:\n",
    "            wc.setdefault(each.lower(),each)\n",
    "            wn.setdefault(devowel(each.lower()),each)\n",
    "        # print(wc,wn)\n",
    "        ans=[]\n",
    "        for each in queries:\n",
    "            if each in wwwww:\n",
    "                ans.append(each)\n",
    "            elif each.lower() in wc:\n",
    "                ans.append(wc[each.lower()])\n",
    "            elif devowel(each.lower()) in wn:\n",
    "                ans.append(wn[devowel(each.lower())])\n",
    "            else:\n",
    "                ans.append(\"\")\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        words1 = {word for word in wordlist}\n",
    "        words2, words3, ws = {}, {}, {'a', 'e', 'i', 'o', 'u'}\n",
    "        for word in wordlist:\n",
    "            tmp = word.lower()\n",
    "            if not tmp in words2: words2[tmp] = word\n",
    "            tmp1 = []\n",
    "            for l in word.lower():\n",
    "                if l in ws: tmp1.append('*')\n",
    "                else: tmp1.append(l)\n",
    "            tmp2 = ''.join(tmp1)\n",
    "            if not tmp2 in words3: words3[tmp2] = word\n",
    "        ans = []\n",
    "        for query in queries:\n",
    "            if query in words1: ans.append(query)\n",
    "            else:\n",
    "                q1 = query.lower()\n",
    "                if q1 in words2: ans.append(words2[q1])\n",
    "                else:\n",
    "                    q2 = []\n",
    "                    for l in q1:\n",
    "                        if l in ws: q2.append('*')\n",
    "                        else: q2.append(l)\n",
    "                    q2 = ''.join(q2)\n",
    "                    if q2 in words3: ans.append(words3[q2])\n",
    "                    else: ans.append('')\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def devowel(word):\n",
    "            return \"\".join(\"*\" if c in \"aeiou\" else c for c in word.lower())\n",
    "\n",
    "        wordset = set(wordlist)\n",
    "\n",
    "        wordlower = {word.lower(): word for word in wordlist[::-1]}\n",
    "        wordvowel = {\n",
    "            # re.sub(r\"[aeiou]\", \"a\", word.lower()): word for word in wordlist[::-1]\n",
    "            devowel(word): word\n",
    "            for word in wordlist[::-1]\n",
    "        }\n",
    "\n",
    "        # 当查询完全匹配单词列表中的某个单词（区分大小写）时，应返回相同的单词。\n",
    "        # 当查询匹配到大小写问题的单词时，您应该返回单词列表中的第一个这样的匹配项。\n",
    "        # 当查询匹配到元音错误的单词时，您应该返回单词列表中的第一个这样的匹配项。\n",
    "        # 如果该查询在单词列表中没有匹配项，则应返回空字符串。\n",
    "        res = []\n",
    "        for word in queries:\n",
    "            if word in wordset:\n",
    "                res.append(word)\n",
    "            elif word.lower() in wordlower:\n",
    "                res.append(wordlower[word.lower()])\n",
    "            else:\n",
    "                # word = re.sub(r\"[aeiou]\", \"a\", word.lower())\n",
    "                word = devowel(word.lower())\n",
    "                if word in wordvowel:\n",
    "                    res.append(wordvowel[word])\n",
    "                else:\n",
    "                    res.append(\"\")\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        ans = [''] * len(queries)\n",
    "        ori = set(wordlist)\n",
    "        vol = set(['a', 'e', 'i', 'o', 'u'])\n",
    "        dic_up, dic_vol = {}, {}\n",
    "        for i, word in enumerate(wordlist) :\n",
    "            tmp_up = word.upper()\n",
    "            if tmp_up not in dic_up :\n",
    "                dic_up[tmp_up] = word\n",
    "\n",
    "            tmp_vol = ''.join([ '#' if w.lower() in vol else w.upper() for w in word])\n",
    "            if tmp_vol not in dic_vol :\n",
    "                dic_vol[tmp_vol] = word\n",
    "\n",
    "        for i, q in enumerate(queries) :\n",
    "            if q in ori :\n",
    "                ans[i] = q\n",
    "            elif q.upper() in  dic_up :\n",
    "                ans[i] = dic_up[q.upper()]\n",
    "            else :\n",
    "                tmp = ''.join([ '#' if w.lower() in vol else w.upper() for w in q])\n",
    "                if tmp in dic_vol :\n",
    "                    ans[i] = dic_vol[tmp] \n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        perfect_match_set = set()\n",
    "        lowercase_match_dict = dict()\n",
    "        vowel_match_dict = dict()\n",
    "        for word in wordlist:\n",
    "            perfect_match_set.add(word)\n",
    "            word_lower = word.lower()\n",
    "            if word_lower not in lowercase_match_dict:\n",
    "                lowercase_match_dict[word_lower] = word\n",
    "            vowel_minimum_lexicographic_order_word = vowel_minimum_lexicographic_order(word)\n",
    "            if vowel_minimum_lexicographic_order_word not in vowel_match_dict:\n",
    "                vowel_match_dict[vowel_minimum_lexicographic_order_word] = word\n",
    "        answer_list = [''] * len(queries)\n",
    "        for index in range(len(queries)):\n",
    "            if queries[index] in perfect_match_set:\n",
    "                answer_list[index] = queries[index]\n",
    "                continue\n",
    "            query_lower = queries[index].lower()\n",
    "            if query_lower in lowercase_match_dict:\n",
    "                answer_list[index] = lowercase_match_dict[query_lower]\n",
    "                continue \n",
    "            vowel_minimum_lexicographic_order_word = vowel_minimum_lexicographic_order(queries[index])\n",
    "            if vowel_minimum_lexicographic_order_word in vowel_match_dict:\n",
    "                answer_list[index] = vowel_match_dict[vowel_minimum_lexicographic_order_word]\n",
    "                continue \n",
    "        return answer_list\n",
    "\n",
    "def vowel_minimum_lexicographic_order(word):\n",
    "    word_lower = word.lower()\n",
    "    answer_list = list(word_lower)\n",
    "    vowel_set = {'a', 'e', 'i', 'o', 'u'}\n",
    "    for index in range(len( word_lower)):\n",
    "        if word_lower[index] in vowel_set:\n",
    "            answer_list[index] = 'a'\n",
    "    return ''.join(answer_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        s = set()\n",
    "        refer_lower = {}\n",
    "        refer_sta = {}\n",
    "        res = []\n",
    "        for word in wordlist:\n",
    "            s.add(word)\n",
    "            wordlow = word.lower()\n",
    "            if wordlow not in refer_lower:\n",
    "                refer_lower[wordlow] = word\n",
    "            wordsta = ''.join(\"*\" if c in \"aeiou\" else c for c in wordlow)\n",
    "            if wordsta not in refer_sta:\n",
    "                refer_sta[wordsta] = word\n",
    "        \n",
    "        for word in queries:\n",
    "            if word in s:\n",
    "                res.append(word)\n",
    "                continue\n",
    "            wordL = word.lower()\n",
    "            if wordL in refer_lower:\n",
    "                res.append(refer_lower[wordL])\n",
    "                continue\n",
    "            wordS = ''.join('*' if c in \"aeiou\" else c for c in wordL)\n",
    "            if wordS in refer_sta:\n",
    "                res.append(refer_sta[wordS])\n",
    "                continue\n",
    "            else:\n",
    "                res.append(\"\")\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        # 这个意味着原因字母和大小写是通用的，简单\n",
    "        q1 = set(wordlist)\n",
    "        q2 = dict()\n",
    "        for w in wordlist:\n",
    "            wl = w.lower()\n",
    "            if wl not in q2:\n",
    "                q2[wl] = w \n",
    "        q3 = dict()\n",
    "        for w in wordlist:\n",
    "            wl = w.lower()\n",
    "            wq = ''.join('*' if c in 'aeiou' else c for c in wl)\n",
    "            if wq not in q3:\n",
    "                q3[wq] = w\n",
    "        \n",
    "        def check(word):\n",
    "            if word in q1:\n",
    "                return word\n",
    "            if word.lower() in q2:\n",
    "                return q2[word.lower()]\n",
    "            wq = ''.join('*' if c in 'aeiou' else c for c in word.lower())\n",
    "            if wq in q3:\n",
    "                return q3[wq]\n",
    "            return ''\n",
    "        \n",
    "        return [check(w) for w in queries]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def replaceVowels(s):\n",
    "            res = list(s)\n",
    "            for i in range(len(res)):\n",
    "                if res[i] in 'AEIOU':\n",
    "                    res[i] = '#'\n",
    "            return \"\".join(res)\n",
    "        res = []\n",
    "        oriset = set(wordlist) \n",
    "        capmap = {} \n",
    "        volmap = {}\n",
    "        for i, word in enumerate(wordlist):\n",
    "            ge_word = replaceVowels(word.upper())\n",
    "            if ge_word not in volmap:\n",
    "                volmap[ge_word] = i \n",
    "            else:\n",
    "                volmap[ge_word] = min(volmap[ge_word], i)\n",
    "\n",
    "            up_word = word.upper() \n",
    "            if up_word not in capmap:\n",
    "                capmap[up_word] = i \n",
    "            else:\n",
    "                capmap[up_word] = min(capmap[up_word], i)\n",
    "\n",
    "        for q in queries:\n",
    "            genericWord = replaceVowels(q.upper())\n",
    "            if q in oriset:\n",
    "                res.append(q)\n",
    "            elif q.upper() in capmap:\n",
    "                i = capmap[q.upper()]\n",
    "                res.append(wordlist[i])\n",
    "            elif genericWord in volmap:\n",
    "                i = volmap[genericWord]\n",
    "                res.append(wordlist[i])\n",
    "            else:\n",
    "                res.append(\"\")\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def replaceVowels(s):\n",
    "            res = list(s)\n",
    "            for i in range(len(res)):\n",
    "                if res[i] in 'AEIOU':\n",
    "                    res[i] = '#'\n",
    "            return \"\".join(res)\n",
    "        res = []\n",
    "        oriset = set(wordlist) \n",
    "        capmap = {} \n",
    "        volmap = {}\n",
    "        for i, word in enumerate(wordlist):\n",
    "            ge_word = replaceVowels(word.upper())\n",
    "            if ge_word not in volmap:\n",
    "                volmap[ge_word] = i \n",
    "            else:\n",
    "                volmap[ge_word] = min(volmap[ge_word], i)\n",
    "\n",
    "            up_word = word.upper() \n",
    "            if up_word not in capmap:\n",
    "                capmap[up_word] = i \n",
    "            else:\n",
    "                capmap[up_word] = min(capmap[up_word], i)\n",
    "\n",
    "        for q in queries:\n",
    "            genericWord = replaceVowels(q.upper())\n",
    "            if q in oriset:\n",
    "                res.append(q)\n",
    "            elif q.upper() in capmap:\n",
    "                i = capmap[q.upper()]\n",
    "                res.append(wordlist[i])\n",
    "            elif genericWord in volmap:\n",
    "                i = volmap[genericWord]\n",
    "                res.append(wordlist[i])\n",
    "            else:\n",
    "                res.append(\"\")\n",
    "         \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\r\n",
    "        \r\n",
    "        s = set(wordlist)\r\n",
    "\r\n",
    "        mp = {w: i for i, w in enumerate(wordlist)}\r\n",
    "        mp2 = defaultdict()\r\n",
    "        for i, w in enumerate(wordlist):\r\n",
    "            if w.lower() not in mp2:\r\n",
    "                mp2[w.lower()] = i \r\n",
    "        def change(s: str) -> str:\r\n",
    "            ww = list(s.lower())\r\n",
    "            for i in range(len(ww)):\r\n",
    "                if ww[i] in 'aeiou':\r\n",
    "                    ww[i] = '*'\r\n",
    "            return  \"\".join(ww)\r\n",
    "        for w in wordlist:\r\n",
    "            ww = change(w)\r\n",
    "            if ww not in mp:\r\n",
    "                mp[ww] = mp[w]\r\n",
    "        res = []\r\n",
    "        for q in queries:\r\n",
    "            if q in s:\r\n",
    "                res.append(q)\r\n",
    "            elif q.lower() in mp2:\r\n",
    "                res.append(wordlist[mp2[q.lower()]])\r\n",
    "            else:\r\n",
    "                qq = change(q)\r\n",
    "                if qq not in mp:\r\n",
    "                    res.append(\"\")\r\n",
    "                else:\r\n",
    "                    res.append(wordlist[mp[qq]])\r\n",
    "        return res \r\n",
    "            \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\r\n",
    "        \r\n",
    "        s = set(wordlist)\r\n",
    "\r\n",
    "        mp = {w: i for i, w in enumerate(wordlist)}\r\n",
    "        mp2 = defaultdict()\r\n",
    "        for i, w in enumerate(wordlist):\r\n",
    "            if w.lower() not in mp2:\r\n",
    "                mp2[w.lower()] = i \r\n",
    "        def change(s: str) -> str:\r\n",
    "            ww = list(s.lower())\r\n",
    "            for i in range(len(ww)):\r\n",
    "                if ww[i] in 'aeiou':\r\n",
    "                    ww[i] = '*'\r\n",
    "            return  \"\".join(ww)\r\n",
    "        for w in wordlist:\r\n",
    "            ww = change(w)\r\n",
    "            if ww not in mp:\r\n",
    "                mp[ww] = mp[w]\r\n",
    "        res = []\r\n",
    "        for q in queries:\r\n",
    "            if q in s:\r\n",
    "                # print(q)\r\n",
    "                res.append(q)\r\n",
    "                continue\r\n",
    "            if q.lower() in mp2:\r\n",
    "                res.append(wordlist[mp2[q.lower()]])\r\n",
    "                continue\r\n",
    "            qq = change(q)\r\n",
    "            if qq not in mp:\r\n",
    "                res.append(\"\")\r\n",
    "            else:\r\n",
    "                res.append(wordlist[mp[qq]])\r\n",
    "        return res \r\n",
    "            \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "      checker = {}\n",
    "\n",
    "      @cache\n",
    "      def desensitize(word):\n",
    "        return \"#\" + word.lower()\n",
    "\n",
    "      @cache\n",
    "      def correct(word):\n",
    "        return \"@\" + word.lower().replace(\"a\", \"_\").replace(\"e\", \"_\").replace(\"i\", \"_\").replace(\"o\", \"_\").replace(\"u\", \"_\")\n",
    "\n",
    "      for word in wordlist:\n",
    "        checker[word] = word\n",
    "        if desensitize(word) not in checker:\n",
    "          checker[desensitize(word)] = word\n",
    "        if correct(word) not in checker:\n",
    "          checker[correct(word)] = word\n",
    "\n",
    "      res = []\n",
    "      for query in queries:\n",
    "        if query in checker:\n",
    "          res.append(query)\n",
    "        elif desensitize(query) in checker:\n",
    "          res.append(checker[desensitize(query)])\n",
    "        elif correct(query) in checker:\n",
    "          res.append(checker[correct(query)])\n",
    "        else:\n",
    "          res.append(\"\")\n",
    "\n",
    "      return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        def tran1(x):\n",
    "            r = []\n",
    "            for i in x:\n",
    "                if ord(i) < 91:\n",
    "                    r.append(ord(i)+32)\n",
    "                else:\n",
    "                    r.append(ord(i))\n",
    "            return tuple(r)\n",
    "        def tran2(x):\n",
    "            r = []\n",
    "            for i in x:\n",
    "                if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u' or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U':\n",
    "                    r.append(0)\n",
    "                elif ord(i) < 91:\n",
    "                    r.append(ord(i)+32)\n",
    "                else:\n",
    "                    r.append(ord(i))\n",
    "            return tuple(r)\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        dic3 = {}\n",
    "        for i in wordlist:\n",
    "            dic1[i] = 1\n",
    "            j = tran1(i)\n",
    "            if j not in dic2:\n",
    "                dic2[j] = i\n",
    "            k = tran2(i)\n",
    "            if k not in dic3:\n",
    "                dic3[k] = i\n",
    "        n = len(queries)\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if queries[i] in wordlist:\n",
    "                ans.append(queries[i])\n",
    "            elif tran1(queries[i]) in dic2:\n",
    "                ans.append(dic2[tran1(queries[i])])\n",
    "            elif tran2(queries[i]) in dic3:\n",
    "                ans.append(dic3[tran2(queries[i])])\n",
    "            else:\n",
    "                ans.append(\"\")\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:\n",
    "        vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "        ori = set(wordlist)\n",
    "        low = {}\n",
    "        blur = defaultdict(dict)\n",
    "        def getCons(w):\n",
    "            return ''.join([c for c in w.lower() if c not in vowels])\n",
    "        def getInd(w):\n",
    "            v_index = [i for i in range(len(w)) if w.lower()[i] in vowels]\n",
    "            ind = 0\n",
    "            for i in v_index:\n",
    "                ind |= 1<<i\n",
    "            return ind\n",
    "        for w in wordlist:\n",
    "            if w.lower() not in low:\n",
    "                low[w.lower()] = w\n",
    "            cons, ind = getCons(w), getInd(w)\n",
    "            if ind not in blur[cons]:\n",
    "                blur[cons][ind] = w\n",
    "        ans = []\n",
    "        for q in queries:\n",
    "            if q in ori:\n",
    "                ans.append(q)\n",
    "            else:\n",
    "                q = q.lower()\n",
    "                if q in low:\n",
    "                    ans.append(low[q])\n",
    "                else:\n",
    "                    cons, ind = getCons(q), getInd(q)\n",
    "                    if cons in blur and ind in blur[cons]:\n",
    "                        ans.append(blur[cons][ind])\n",
    "                    else:\n",
    "                        ans.append('')\n",
    "        return ans "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
